﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using DBUtility;
using MySql.Data.MySqlClient;
using Model;

namespace DAL
{
    public class DDevice
    {
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string emi)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from tdevice");
            strSql.Append(" where Emi=@Emi");
            MySqlParameter[] parameters = {
					new MySqlParameter("@Emi", MySqlDbType.String)
};
            parameters[0].Value = emi;

            return DbHelperMySQL.Exists(strSql.ToString(), parameters);
        }



        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(Model.DeviceInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into tdevice(");
            strSql.Append("Emi,PhoneNumber,Password,RegistTime,Type)");
            strSql.Append(" values (");
            strSql.Append("@Emi,@PhoneNumber,@Password,@RegistTime,@Type)");            
            MySqlParameter[] parameters = {
					new MySqlParameter("@Emi", MySqlDbType.VarChar,20),
					new MySqlParameter("@PhoneNumber", MySqlDbType.VarChar,20),
					new MySqlParameter("@Password", MySqlDbType.VarChar,20),
                    new MySqlParameter("@RegistTime", MySqlDbType.DateTime),
                    new MySqlParameter("@Type", MySqlDbType.VarChar,30)
				 };
            parameters[0].Value = model.Emi;
            parameters[1].Value = model.PhoneNumber;
            parameters[2].Value = model.Password;
            parameters[3].Value = DateTime.Now;
            parameters[4].Value = model.Type;
           
            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.DeviceInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update tdevice set ");
            strSql.Append("PhoneNumber=@PhoneNumber,");
            strSql.Append("Password=@Password,");
            strSql.Append("UpdateTime=@UpdateTime,");
            strSql.Append("Type=@Type");
            strSql.Append(" where Emi=@Emi");
            MySqlParameter[] parameters = {
					new MySqlParameter("@PhoneNumber", MySqlDbType.VarChar,20),
					new MySqlParameter("@Password", MySqlDbType.VarChar,20),
                    new MySqlParameter("@UpdateTime", MySqlDbType.DateTime),
                    new MySqlParameter("@Emi", MySqlDbType.VarChar,20),
                    new MySqlParameter("@Type", MySqlDbType.VarChar,30)};
            parameters[0].Value = model.PhoneNumber;
            parameters[1].Value = model.Password;
            parameters[2].Value = model.UpdateTime;
            parameters[3].Value = model.Emi;
            parameters[4].Value = model.Type;
            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
 
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM tdevice ");
            if (strWhere!="")
            {
                strSql.Append(strWhere);
            }
            return DbHelperMySQL.Query(strSql.ToString());
        }


    }
}
